<template>
  <div>
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>费用统计
        <i class="el-icon-arrow-right"></i> 客户消费统计
      </el-breadcrumb-item>
    </el-breadcrumb> -->
    <div class="title">
      <span>全部账号</span>
      <div>
        <!-- <el-button type="primary" @click="reset">重置密码</el-button> -->
        <el-button
          type="primary"
          v-if="rolesId == 1 || rolesId == 5 || rolesId == 6 || rolesId == 8"
          @click="addAccount"
          >新增账号</el-button
        >
        <el-button type="primary" v-if="oemFlag == 1" @click="addAccount"
          >新增账号</el-button
        >
        <!-- <el-button icon="el-icon-refresh" @click="refresh"></el-button> -->
      </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
          <div class="leftitem">
            <div class="leftitem" >
              <el-select
                v-model="serviceinfo.state"
                placeholder="用户类型"
                @change="allUserTypes"
              >
                <el-option
                  v-for="item in allUserType"
                  :label="item.rolename"
                  :value="item.id"
                  :key="item.value"
                ></el-option>
              </el-select>
              <el-input
                class="merchants"
                v-model.trim="serviceinfo.userName"
                @keyup.enter.native="soso"
                placeholder="用户名"
              ></el-input>
            </div>
          </div>
        </div>
        <div class="rightBox">
          <div class="rightitem">
            <el-input
              class="merchants"
              v-model.trim="serviceinfo.phone"
              @keyup.enter.native="soso"
              placeholder="手机号"
            ></el-input>
            <el-input
              class="merchants"
              v-model.trim="serviceinfo.agent"
              placeholder="昵称"
              @keyup.enter.native="soso"
            ></el-input>
          </div>
          <div class="rightitem">
            <el-button
              icon="el-icon-search"
              type="primary"
              @click="soso"
              :disabled="isDisable"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" type="primary" @click="reset"
              >重置</el-button
            >
          </div>
        </div>
      </div>
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="merchantarr"
          v-loading="loading"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column
            type="index"
            align="center"
            width="50"
            label="序号"
          ></el-table-column>
          <el-table-column type="selection"></el-table-column>
          <el-table-column
            prop="username"
            align="center"
            label="用户名"
          ></el-table-column>
          <el-table-column
            prop="nickName"
            align="center"
            label="昵称"
          ></el-table-column>
          <el-table-column
            prop="phone"
            align="center"
            label="手机号"
          ></el-table-column>
          <el-table-column
            prop="email"
            align="center"
            label="邮箱"
          ></el-table-column>
          <el-table-column
            prop="createtime"
            align="center"
            label="创建时间"
          ></el-table-column>
          <el-table-column align="center" label="用户类型">
            <template slot-scope="scope">
              <div>
                {{ scope.row.subUser == 1 ? "子账号" : scope.row.rolesName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="typeName"
            align="center"
            label="是否系统用户"
          ></el-table-column>
          <el-table-column
            prop="bindstatus"
            align="center"
            label="是否绑定手机号"
          >
            <template slot-scope="scope">
              <span
                v-show="
                  scope.row.bindstatus == null || scope.row.bindstatus == 0
                "
                >未绑定</span
              >
              <span v-show="scope.row.bindstatus == 1">已绑定</span>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="180"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                v-if="
                  rolesId == 1 || rolesId == 5 || rolesId == 6 || rolesId == 8
                "
                @click="changePassword(scope.row)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                style="color: red"
                v-if="scope.row.status !== 98"
                @click="disable(scope.row.id)"
                >停用</el-button
              >
              <el-button
                type="text"
                style="color: green"
                v-show="scope.row.status == 98"
                @click="enable(scope.row.id)"
                >启用</el-button
              >
              <el-button
                type="text"
                size="small"
                style="color: orange"
                v-if="scope.row.type == 1"
                @click="postdelete(scope.row.id)"
                >注销</el-button
              >
              <el-button
                type="text"
                size="small"
                v-if="
                  rolesId == 1 || rolesId == 5 || rolesId == 6 || rolesId == 8
                "
                @click="resetPassword(scope.row.id)"
                >重置密码</el-button
              >
              <el-button
                type="text"
                size="small"
                v-if="rolesId == 1"
                @click="PermissionConfig(scope.row.id)"
                >权限配置</el-button
              >
              <el-button
                type="text"
                size="small"
                v-if="rolesId == 1"
                @click="DisableEnableMenu(scope.row.id)"
                >禁用启用菜单</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 20, 30, 40, 50]"
            :page-size="cur_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total1"
          ></el-pagination>
        </div>
      </div>
      <br />
      <br />
      <!-- 权限配置 -->
      <el-dialog v-dialogDrag :visible.sync="DisableMenuShow" width="45%">
        <div class="titleStyle">
          <span class="jinyongcaidan">禁用启用菜单</span>
          <span class="gouxuan">（勾选代表禁用）</span>
        </div>
        <el-tree
          :data="permissions"
          default-expand-all
          show-checkbox
          node-key="id"
          ref="tree"
          :default-expanded-keys="[1, 2, 8, 21, 30, 36]"
          :default-checked-keys="idLists"
          :props="defaultProps"
        >
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="DisableMenuShow = false">取消</el-button>
          <el-button @click="updateUserMenuIsEnable">确定</el-button>
        </span>
      </el-dialog>
      <!-- 修改账号 -->
      <el-dialog
        v-dialogDrag
        title="修改账号"
        :visible.sync="visible"
        width="30%"
        :close-on-click-modal="false"
        @closed="subClose"
      >
        <el-form
          :model="accountalter"
          status-icon
          ref="accountalter"
          label-width="110px"
          class="demo-dynamic"
        >
          <el-form-item :required="true" label="用户名">
            <div>{{ changUser.username }}</div>
          </el-form-item>
          <div class="change-sub-mer-box">
            <el-form-item :required="true" label="用户类型">
              <div>{{ changUser.userType }}</div>
            </el-form-item>
            <el-button
              type="primary"
              class="sub-mer-btn"
              @click="subMerconfig(changUser.id)"
              v-if="changeSubUser"
              >配置子账号菜单</el-button
            >
          </div>
          <el-form-item :required="true" label="昵称">
            <el-input
              type="text"
              v-model.trim="changUser.nickName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="手机号">
            <el-input
              type="text"
              v-model.trim="changUser.phone"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="邮箱">
            <el-input
              type="text"
              v-model.trim="changUser.email"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item :required="true" label="密码">
            <el-input type="text" v-model="accountalter.contact" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item :required="true" label="服务密码">
            <el-input type="text" v-model="accountalter.contact" autocomplete="off"></el-input>
          </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <!-- @click="visible = false" -->
          <el-button type="primary" @click="updateUser(changUser.id)"
            >确定</el-button
          >
        </span>
      </el-dialog>
      <!-- 新增账号 -->
      <el-dialog
        v-dialogDrag
        title="新增账号"
        :visible.sync="addaccount"
        width="40%"
        :close-on-click-modal="false"
        @closed="subClose"
      >
        <el-form
          :model="accountalter"
          status-icon
          ref="accountalter"
          label-width="110px"
          class="demo-dynamic"
        >
          <el-form-item :required="true" label="用户类型">
            <el-select
              class="select-input"
              v-model="addUserdata.id"
              placeholder="用户类型"
              @change="changusertype"
            >
              <el-option
                v-for="(item, index) in getAllUserType"
                :label="item.rolename"
                :value="item.id"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            :required="true"
            label="商户名"
            v-if="
              (addUserdata.id == 5 || addUserdata.id == 6) &&
              rolesId !== 5 &&
              rolesId !== 6
            "
          >
            <el-autocomplete
              class="inline-input"
              v-model="state1"
              :fetch-suggestions="querySearch"
              placeholder="请输入商户名"
              @select="handleSelect"
              :debounce="1000"
              @change="fuzzyQuery"
            ></el-autocomplete>
          </el-form-item>
          <el-form-item
            :required="true"
            label="商户下属门店"
            v-if="addUserdata.id == 6 && rolesId !== 6"
          >
            <el-select
              class="select-input"
              v-model="storeList.id"
              placeholder="商户下属门店"
              @change="changusertype1"
            >
              <el-option
                v-for="item in storeList"
                :label="item.shopName"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <div class="sub-mer-box" v-if="addUserdata.id == 5">
            <el-form-item label="是否子账号">
              <el-checkbox
                v-model.trim="isSubMer"
                @change="subMerChane"
              ></el-checkbox>
            </el-form-item>
            <el-button
              type="primary"
              class="sub-mer-btn"
              @click="subMerconfig"
              v-if="isSubMer"
              >配置子账号菜单</el-button
            >
          </div>
          <el-form-item :required="true" label="用户名">
            <el-input
              type="text"
              v-model.trim="addUserdata.userName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="昵称">
            <el-input
              type="text"
              v-model.trim="addUserdata.nickName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="手机号">
            <el-input
              type="text"
              v-model.trim="addUserdata.phone"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="邮箱">
            <el-input
              type="text"
              v-model.trim="addUserdata.email"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="密码">
            <el-input
              type="text"
              v-model.trim="addUserdata.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item :required="true" label="服务密码">
            <el-input
              type="text"
              v-model.trim="addUserdata.servicePassword"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addaccount = false">取消</el-button>
          <el-button type="primary" @click="addUser()">确定</el-button>
        </span>
      </el-dialog>

      <!-- 子账号权限菜单 -->
      <el-dialog
        v-dialogDrag
        title="子账号权限配置"
        :visible.sync="eqma"
        width="45%"
      >
        <el-tree
          :data="permissions"
          default-expand-all
          show-checkbox
          node-key="id"
          ref="tree"
          :default-expanded-keys="[1, 2, 8, 21, 30, 36]"
          :default-checked-keys="idLists"
          :props="defaultProps"
          append-to-body
        >
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="eqma = false">取消</el-button>
          <el-button type="primary" @click="getCheckedKeys">确定</el-button>
        </span>
      </el-dialog>

      <!-- 新增账号成功弹框 -->
      <el-dialog
        v-dialogDrag
        title="新增账号成功"
        :visible.sync="addaccount1"
        width="30%"
        center
        :close-on-click-modal="false"
      >
        <div class="center center1">
          <i class="el-icon-success"></i>
          <span>账号创建成功！</span>
        </div>
        <div class="center">您的账号/用户名：123456</div>
        <div class="center">密码：123456</div>
        <div class="center">服务密码：123456</div>
        <div class="juzhong">*服务密码用于交易退款，请妥善保管！</div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addaccount1 = false"
            >好 的</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  APIupdateUserMenuIsEnable,
  APIgetUserMenuIsEnable,
  APIgetUserMenu,
  getAllMenu,
  APIPermissionConfiguration,
  agent,
  getAccountList,
  getUserTypes,
  getAddUserTypes,
  addAccount,
  disableUser,
  resetPassword,
  updateUser,
  getUserSelect,
  getStoreList,
  postUserList,
  enableUser,
  postuserdelete,
  getOwnedMenu,
} from "@/api/api";
import {
  getSubUserOwnedMenu,
  saveSubMer,
  updateSubMerMenu,
} from "@/api/account/account.js";
export default {
  name: "baseform",
  data: function () {
    return {
      tempArray: [],
      DisableMenuShow: false,
      defaultProps: {
        children: "children",
        label: "label",
      },
      roleId: "",
      eqma: false,
      loading: true,
      isDisable: false,
      //权限ID
      rolesId: null,
      fid: null,
      mid: null,
      total1: 0,
      cur_page: 1,
      cur_size: 10,
      multipleSelection: [],
      merchantarr: [],
      pageCount: "",
      visible: false,
      addaccount: false,
      addaccount1: false,
      serviceinfo: {
        // stardate:"",
        // enddate:"",
        userName: "",
        phone: "",
        agent: "",
        state: "",
      },
      accountalter: {},
      allUserType: [],
      //新增用户 获取用户类型列表
      getAllUserType: [],
      idList: [],

      isSubMer: false,
      permissions: [],
      idLists: [],
      subMerList: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      eqma: false,
      changeSubUser: null,
      //新增用户 数据
      addUserdata: {
        id: "",
        userName: "",
        nickName: "",
        phone: "",
        email: "",
        password: "",
        servicePassword: "",
      },
      changUser: {
        username: "",
        userType: "",
        nickName: "",
        phone: "",
        email: "",
        id: null,
      },
      //新增框 模糊查询
      restaurants: [{ value: "", id: "" }],
      state1: "",
      state2: "",
      inputName: "",
      //门店列表
      storeList: [],
      oemFlag: "",
    };
  },
  created() {
    // this.agent();
    this.oemFlag = sessionStorage.getItem("oemFlag");
    this.getPermissions();
    this.getUserTypes();
    

    // console.log(this.rolesId)
  },
  mounted() {
    window.a = this;
  },
  methods: {
    //修改thead 背景色
    tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;";
      }
    },
    //分页导航
    handleSizeChange(val) {
      // console.log(val)
      // console.log(this.rolesId)
      this.cur_size = val;
      if (this.rolesId !== 5) {
        this.getData(this.serviceinfo.state);
      } else if (this.rolesId == 5) {
        this.getData(this.serviceinfo.state);
      }
    },
    handleCurrentChange(val) {
      console.log(this.serviceinfo.state);
      this.cur_page = val;
      if (this.rolesId !== 5) {
        this.getData(this.serviceinfo.state);
      } else if (this.rolesId == 5) {
        this.getData(this.serviceinfo.state);
      }
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //获取权限
    getPermissions() {
      var aaa = sessionStorage.getItem("userInfo");
      var bbb = JSON.parse(aaa);
      this.rolesId = bbb.rolesId;
      // this.serviceinfo.state = this.rolesId;
      this.fid = bbb.fid;
      this.mid = bbb.mid;
    },
    //获取账号列表
    getData(val) {
      console.log('val',val);
      this.isDisable = true;
      getAccountList({
        page: this.cur_page,
        size: this.cur_size,
        keywords: this.serviceinfo.userName,
        phone: this.serviceinfo.phone,
        // rolesId: this.serviceinfo.state,
        rolesId: this.serviceinfo.state,
        nickName: this.serviceinfo.agent,
      }).then((res) => {
        if (res.code == 405) {
          localStorage.removeItem("token");
          this.$router.push("/login");
        } else if (res.code == 200) {
          this.isDisable = false;
          this.loading = false;
          this.merchantarr = res.data;
          this.total1 = res.pageInfo.recordCount;
          this.pageCount = res.pageInfo.pageCount;
        } else if (res.code == 500) {
          this.isDisable = false;
          this.loading = false;
          this.$message.error(res.message);
        }
      });
      getAddUserTypes({}).then((res) => {
        this.getAllUserType = [];
        for (var i = 0; i < res.data.length; i++) {
          this.getAllUserType.push({
            value: res.data[i].rolename,
            id: res.data[i].id,
          });
        }
        // this.getAllUserType = res.data
        // console.log(this.getAllUserType)
      });
      if (this.fid !== null) {
        getStoreList({
          merchantId: this.fid,
        }).then((res) => {
          if (res.code == 200) {
            // this.storeList = []
            // for(var i=0;i<res.data.length;i++){
            //   this.storeList.push({value:res.data.rows[i].shopName,id:res.data.rows[i].id})
            // }
            this.storeList = res.data.rows;
            // console.log(this.storeList)
          } else {
            // console.log(this.roleId)
          }
        });
      }
    },
    // 搜索
    soso() {
      this.loading = true;
      this.cur_page = 1;
      if (this.serviceinfo.state !== null) {
        this.getData(this.serviceinfo.state);
      } else {
        this.getData();
      }
    },
    // 重置
    reset() {
      this.serviceinfo.phone = "";
      this.serviceinfo.userName = "";
      // this.serviceinfo.state= ''
      this.serviceinfo.agent = "";
      if (this.rolesId == 5) {
        this.serviceinfo.state = "";
      }
      this.serviceinfo.state = "";
    },
    // 刷新
    refresh() {
      // location.reload()
      // this.$router.replace({
      //   path: '/refresh',
      //   query: {
      //     t: Date.now()
      //   }
      // })
      this.loading = true;
      this.channellist();
    },
    //获取用户类型下拉列表
    getUserTypes() {
      const list = [ { id:1,rolename:'超级管理员' } , { id:2,rolename:'一级服务商' } , { id:3,rolename:'二级服务商' }, { id:4,rolename:'业务经理' }, { id:5,rolename:'商户' }, { id:6,rolename:'门店' }]
      if(this.rolesId == 1 || this.rolesId == 8){
        this.allUserType = [ { id:1,rolename:'超级管理员' } , { id:2,rolename:'一级服务商' } , { id:3,rolename:'二级服务商' }, { id:4,rolename:'业务经理' }, { id:5,rolename:'商户' }, { id:6,rolename:'门店' }, { id:8,rolename:'运营' }, { id:9,rolename:'财务' }]
        this.getData()
      }else{
        const index = list.findIndex(item=>item.id == this.rolesId)
      if(index != -1){
        this.allUserType = list.splice(index + 1 , list.length - 1)
        this.serviceinfo.state = this.allUserType[0].id
        if (this.rolesId !== 5) {
      this.getData();
    } else if (this.rolesId == 5) {
      this.getData();
      this.serviceinfo.state = "";
    }
      }
      
      }
      // getUserTypes({}).then((res) => {
      //   if (res.code == 405) {
      //     localStorage.removeItem("token")
      //     this.$router.push("/login")
      //   } else {
      //     this.allUserType = res.data
      //     this.serviceinfo.state = res.data[0].id
      //   }
      // })
    },
    //用户类型下拉列表
    allUserTypes() {
      // this.cur_page = 1
      // this.getData()
    },
    //新增账号 获取用户类型列表数据
    addAccount() {
      this.addaccount = true;
      this.addUserdata.id = "";
      this.addUserdata.userName = "";
      this.addUserdata.nickName = "";
      this.addUserdata.phone = "";
      this.addUserdata.email = "";
      this.addUserdata.password = "";
      this.addUserdata.servicePassword = "";
      this.state1 = "";
      this.storeList.id = "";

      // else if(this.storeList.length !== 0){
      //   this.addUserdata.id = this.storeList[0].id
      // }
      // console.log(this.addUserdata.id)
      getAddUserTypes({}).then((res) => {
        this.getAllUserType = res.data;
        // console.log(this.getAllUserType)
      });
      if (this.getAllUserType.length !== 0) {
        this.addUserdata.id = this.getAllUserType[0].id;
      }
      if (this.rolesId == 5 && this.fid !== null) {
        getStoreList({
          merchantId: this.fid,
        }).then((res) => {
          if (res.code == 200) {
            this.storeList = [];
            // for(var i=0;i<res.data.length;i++){
            //   this.storeList.push({value:res.data.rows[i].shopName,id:res.data.rows[i].id})
            // }
            this.storeList = res.data.rows;
          }
        });
      }
    },
    // 是否子账号
    subMerChane(e) {
      if (e) {
        console.log("是子账号");
        this.getSubMerRole();
      }
    },
    // 配置子账号菜单
    subMerconfig() {
      this.eqma = true;
    },
    // 获取子账号的商户模板菜单
    async getSubMerRole(id) {
      let res =
        typeof id == "number"
          ? await getSubUserOwnedMenu({ userId: id })
          : await getSubUserOwnedMenu();
      if (res.code == 405) {
        sessionStorage.removeItem("token");
        this.$router.push("/login");
      } else {
        if (res.code == 200) {
          this.permissions = res.data.allMenu;
          this.idList = res.data.idList;
          let array1 = [];
          for (var i = 0; i < this.permissions.length; i++) {
            array1.push(this.permissions[i].id);
          }
          //临时数组存放
          let tempArray1 = []; //临时数组1
          let tempArray2 = []; //临时数组2
          for (var i = 0; i < array1.length; i++) {
            tempArray1[array1[i]] = true; //将数array2 中的元素值作为tempArray1 中的键，值为true；
          }
          for (var i = 0; i < this.idList.length; i++) {
            if (!tempArray1[this.idList[i]]) {
              tempArray2.push(this.idList[i]); //过滤array1 中与array2 相同的元素；
            }
          }
          this.idList.length == 0
            ? (this.idLists = array1)
            : (this.idLists = tempArray2);
        } else {
          this.$message.error(res.message);
        }
      }
    },
    getCheckedKeys() {
      let list = this.$refs.tree.getCheckedKeys();
      list = list.concat(this.$refs.tree.getHalfCheckedKeys());
      this.subMerList = list;
      this.eqma = false;
    },
    subClose() {
      this.subMerList = [];
      this.idList = [];
      this.allMenu = [];
    },
    //新增账号
    addUser() {
      if (this.rolesId == 1 && this.addUserdata.id == 5) {
        //如果rolesId == 1超管登陆 且 addUserdata.id == 5选择添加商户
        // this.rolesId = this.addUserdata.id //新增时rolesId为用户类型选中时的id
        // this.state1 = this.state2 //新增时mid为商户名匹配的id    state2是用来存储模糊查询选中时的id
        this.storeList.id = this.state2; //因为新增的是商户账户 fid 等于选择的mid
      } else if (this.rolesId == 1 && this.addUserdata.id == 6) {
        //如果rolesId == 1超管登陆 且 addUserdata.id == 6选择添加门店
        // this.rolesId = this.addUserdata.id
        // this.state1 = this.state2
      } else if (this.rolesId == 5 && this.addUserdata.id == 5) {
        //如果rolesId == 5商户账号登陆 且 addUserdata.id == 5选择添加自己的商户号
        this.storeList.id = this.fid; //如果商户号登陆 添加自身的商户账户 fid 和 mid 等于 本身的
        this.state2 = this.mid;
      } else if (this.rolesId == 5 && this.addUserdata.id == 6) {
        this.state2 = this.mid;
      } else if (this.rolesId == 6) {
        this.state2 = this.mid;
        this.storeList.id = this.fid;
      } else if (this.rolesId == 2) {
        this.storeList.id = this.fid;
      }
      if (this.addUserdata.id == 5 && this.isSubMer) {
        let obj = {
          idList: this.subMerList,
          params: {
            rolesId: this.addUserdata.id,
            username: this.addUserdata.userName,
            nickName: this.addUserdata.nickName,
            phone: this.addUserdata.phone,
            email: this.addUserdata.email,
            password: this.addUserdata.password,
            servicePassword: this.addUserdata.servicePassword,
            mid: this.state2,
            fid: this.storeList.id,
            subUser: 1,
          },
        };
        if (this.subMerList.length == 0) {
          this.permissions.forEach((val) => {
            this.subMerList.push(val.id);
            if (val.children) {
              for (let k in val.children) {
                this.subMerList.push(val.children[k].id);
                if (val.children[k].children.length > 0) {
                  for (let j in val.children[k].children) {
                    this.subMerList.push(val.children[k].children[j].id);
                  }
                }
              }
            }
          });
        }
        saveSubMer(obj).then((res) => {
          if (res.code == 405) {
            localStorage.removeItem("token");
            this.$router.push("/login");
          } else {
            if (res.code == 200) {
              this.addUserdata.id = "";
              this.addUserdata.userName = "";
              this.addUserdata.nickName = "";
              this.addUserdata.phone = "";
              this.addUserdata.email = "";
              this.addUserdata.password = "";
              this.addUserdata.servicePassword = "";
              this.state1 = "";
              this.storeList.id = "";
              this.getData(this.serviceinfo.state);
              this.addaccount = false;
              this.restaurants = [];
              this.subClose();
              this.$message.success("新增成功");
            } else {
              this.$message.error(res.message);
            }
          }
        });
      } else {
        postUserList({
          rolesId: this.addUserdata.id,
          // type:this.addUserdata.id,
          username: this.addUserdata.userName,
          nickName: this.addUserdata.nickName,
          phone: this.addUserdata.phone,
          email: this.addUserdata.email,
          password: this.addUserdata.password,
          servicePassword: this.addUserdata.servicePassword,
          mid: this.state2,
          fid: this.storeList.id,
        }).then((res) => {
          // console.log(res)
          if (res.code == 200) {
            this.addUserdata.id = "";
            this.addUserdata.userName = "";
            this.addUserdata.nickName = "";
            this.addUserdata.phone = "";
            this.addUserdata.email = "";
            this.addUserdata.password = "";
            this.addUserdata.servicePassword = "";
            this.state1 = "";
            this.storeList.id = "";
            this.getData(this.serviceinfo.state);
            this.addaccount = false;
            this.restaurants = [];
          } else if (res.code == 500) {
            // this.storeList.id = ''
            // this.state1 = ''
            this.$message.error(res.message);
            console.log(this.storeList.id);
            console.log(this.state1);
          }
        });
      }
    },
    //商户名 模糊查询
    fuzzyQuery(e) {},
    querySearch(queryString, cb) {
      var restaurants = this.restaurants;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
      getUserSelect({
        //商户模糊查询
        roleId: 5,
        keywords: this.state1,
      }).then((res) => {
        // console.log(res.data)
        if (res.code == 200) {
          this.restaurants = [];
          for (var i = 0; i < res.data.length; i++) {
            this.restaurants.push({
              value: res.data[i].fname,
              id: res.data[i].fid,
            });
          }
          let results = queryString
            ? this.restaurants.filter(this.createFilter(queryString))
            : this.restaurants;
          // 调用 callback 返回建议列表的数据
          cb(results);
        }
      });
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1
        );
      };
    },
    handleSelect(item) {
      this.state2 = item.id;
      console.log(item.id);
      getStoreList({
        merchantId: item.id,
      }).then((res) => {
        if (res.code == 200) {
          this.storeList = [];
          // console.log(res.data.rows)
          // for(var i=0;i<res.data.length;i++){
          //   this.storeList.push({'value':res.data.rows[i].shopName,'id':res.data.rows[i].id})
          // }
          this.storeList = res.data.rows;
          // console.log(this.storeList)
        }
      });
      // this.state1 = item.id
    },
    changusertype() {
      this.isSubMer = false;
      this.idList = [];
      this.subMerList = [];
      this.allMenu = [];
      // console.log(this.addUserdata.id)
      // getUserSelect({
      //   roleId: 5,
      //   keywords: this.state1
      // }).then(res => {
      //   console.log(res.data)
      //   if(res.code == 200){
      //     for(var i=0;i<res.data.length;i++){
      //       this.restaurants.push({'value':res.data[i].fname,'id':res.data[i].fid})
      //     }
      //   }
      // })
    },
    changusertype1() {
      // console.log(this.storeList.id)
    },
    //修改账户
    changePassword(val) {
      this.visible = true;
      this.changUser.username = val.username;
      this.changUser.userType = val.subUser == 1 ? "子账号" : val.rolesName;
      this.changUser.nickName = val.nickName;
      this.changUser.phone = val.phone;
      this.changUser.email = val.email;
      this.changUser.id = val.id;
      this.changeSubUser = val.subUser;
      this.getSubMerRole(val.id);
    },
    //修改更新
    updateUser(val) {
      if (this.changeSubUser) {
        if (this.subMerList.length > 0) {
          updateSubMerMenu({
            idList: this.subMerList,
            params: {
              userId: this.changUser.id,
            },
          }).then((res) => {
            if (res.code == 405) {
              localStorage.removeItem("token");
              this.$router.push("/login");
            } else {
              if (res.code == 200) {
              } else {
                this.$message.error("配置子账号菜单出错：" + res.message);
              }
            }
          });
        }
      }
      updateUser({
        id: val,
        fid: this.fid,
        mid: this.mid,
        username: this.changUser.username,
        nickName: this.changUser.nickName,
        phone: this.changUser.phone,
        email: this.changUser.email,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.visible = false;
          if (this.rolesId !== 5) {
            this.getData(this.serviceinfo.state);
          } else if (this.rolesId == 5) {
            this.getData();
          }
        } else if (res.code == 500) {
          this.$message.error(res.message);
        }
      });
    },
    //停用
    disable(val) {
      this.$confirm(`是否确定停用该账号？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableUser({}, val).then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.getData(this.serviceinfo.state);
            } else if (res.code == 500) {
              this.$message.error(res.message);
              this.getData(this.serviceinfo.state);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //启用
    enable(val) {
      this.$confirm(`是否确定启用该账号？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableUser({}, val).then((res) => {
            this.$message({
              message: "修改成功",
              type: "success",
            });
            if (this.rolesId !== 5) {
              this.getData(this.serviceinfo.state);
            } else if (this.rolesId == 5) {
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //删除
    postdelete(val) {
      this.$confirm(`是否确定注销该账号？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postuserdelete({}, val).then((res) => {
            this.$message({
              message: "注销成功",
              type: "success",
            });
            if (this.rolesId !== 5) {
              this.getData(this.serviceinfo.state);
            } else if (this.rolesId == 5) {
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //重置密码
    resetPassword(val) {
      console.log(val);
      this.$confirm(
        "如果选择重置，您的登陆密码将被重置为“123456”!",
        "重置密码",
        {
          confirmButtonText: "重置",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          resetPassword({}, val).then((res) => {
            this.$message({
              type: "success",
              message: "重置成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //权限配置
    PermissionConfig(val) {
      console.log(val);
      this.eqma = true;
      this.roleId = val;
      APIgetUserMenu({
        userId: val,
      }).then((res) => {
        console.log("APIgetUserMenu=", res);
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else {
          this.permissions = res.data.allMenu;
          this.idList = res.data.idList;
          let array1 = [];
          for (var i = 0; i < this.permissions.length; i++) {
            array1.push(this.permissions[i].id);
          }
          //临时数组存放
          let tempArray1 = []; //临时数组1
          let tempArray2 = []; //临时数组2
          for (var i = 0; i < array1.length; i++) {
            tempArray1[array1[i]] = true; //将数array2 中的元素值作为tempArray1 中的键，值为true；
          }
          for (var i = 0; i < this.idList.length; i++) {
            if (!tempArray1[this.idList[i]]) {
              tempArray2.push(this.idList[i]); //过滤array1 中与array2 相同的元素；
            }
          }
          // console.log(tempArray2)
          this.idLists = tempArray2;
        }
      });
    },
    getCheckedKeys() {
      let list = this.$refs.tree.getCheckedKeys();
      list = list.concat(this.$refs.tree.getHalfCheckedKeys());
      let lists = list.join(",");
      this.updateRoleMenu(lists);
    },
    // 修改角色权限
    updateRoleMenu(idList) {
      APIPermissionConfiguration({
        userId: this.roleId,
        idList: idList,
      }).then((res) => {
        console.log(res);
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if (res.code == 200) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.eqma = false;
          this.getAllMenu();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 更新菜单
    getAllMenu() {
      let userinfo = JSON.parse(sessionStorage.getItem("userInfo"));
      getAllMenu({
        roleId: userinfo.rolesId,
      }).then((res) => {
        sessionStorage.setItem("menuList", JSON.stringify(res.data));
      });
    },
    // 禁用启用菜单
    DisableEnableMenu(val) {
      console.log(val);
      this.DisableMenuShow = true;
      this.roleId = val;
      APIgetUserMenuIsEnable({
        userId: val,
      }).then((res) => {
        console.log("APIgetUserMenuIsEnable=", res);
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else {
          this.permissions = res.data.allMenu;
          this.idList = res.data.idList;
          let array1 = [];
          for (var i = 0; i < this.permissions.length; i++) {
            array1.push(this.permissions[i].id);
          }
          console.log("array1==", array1);
          //临时数组存放
          let tempArray1 = []; //临时数组1
          let tempArray2 = []; //临时数组2
          for (var i = 0; i < array1.length; i++) {
            tempArray1[array1[i]] = true; //将数array2 中的元素值作为tempArray1 中的键，值为true；
          }
          for (var i = 0; i < this.idList.length; i++) {
            if (!tempArray1[this.idList[i]]) {
              tempArray2.push(this.idList[i]); //过滤array1 中与array2 相同的元素；
            }
          }

          this.idLists = tempArray2;
        }
      });
    },
    updateUserMenuIsEnable() {
      let list = this.$refs.tree.getCheckedKeys();
      let lists = list.join(",");
      this.disableRoleMenu(lists);
    },
    // 禁用角色权限
    disableRoleMenu(idList) {
      APIupdateUserMenuIsEnable({
        userId: this.roleId,
        idList: idList,
      }).then((res) => {
        console.log(res);
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if (res.code == 200) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.DisableMenuShow = false;
          this.getAllMenu();
        } else {
          this.$message.error(res.message);
        }
      });
    },
  },
};
</script>
<style scoped lang="less">
.titleStyle {
  font-size: 20px;
  margin-bottom: 30px;
}
.gouxuan {
  color: #f9607d;
}
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.name {
  width: 25%;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 50%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.merchants {
  width: 40%;
}
.statistical {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #00152a;
  color: #fff;
  padding: 12px;
  box-sizing: border-box;
}
.statistical > .statisticalitem {
  width: 50%;
  text-align: center;
  line-height: 200%;
}
.tableBox {
  margin-top: 20px;
}

.red {
  color: #f9607d;
}

.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;

    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .addr {
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
.juzhong {
  text-align: center;
  color: red;
}
.center {
  text-align: center;
  margin-bottom: 10px;
}
.center1 > i {
  color: #66b1ff;
  height: 50px;
  font-size: 50px;
  line-height: 50px;
  // position: absolute;
  margin: 0 -100px;
  margin-right: 55px;
  text-align: center;
}
.center1 {
  height: 50px;
  position: relative;
}
.center1 > span {
  height: 50px;
  position: absolute;
  font-size: 18px;
  margin: 0 -35px;
  line-height: 50px;
}
.inline-input {
  width: 100%;
}
.select-input {
  width: 100%;
}
.sub-mer-box {
  display: flex;
  width: 100%;
}
.change-sub-mer-box {
  display: flex;
}
.sub-mer-btn {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 20px;
  width: 100px;
  height: 30px;
}
/deep/.el-dialog {
  top: 5% !important;
  left: 0 !important;
}
</style>